<template>
	<scroll-view class="form-container bg" :scroll-y="true">
        <blueList
            @val="getBlueVal"
            :print="printRandom"
            :printData="printData"
            @print-finish="confirmLoading = false"
            @device-state="getDeviceState"
        ></blueList>
        <view v-if="activeTab === 0" class="margin-top-10">
            <jkFormCard title="基本信息" :show-right-icon="true">
                <u-icon slot="right" name="scan" size="24" @click="onScanBtn"></u-icon>
                <jk-form-item label="计划单" :show-right-icon="false">
                    <view>{{ ruleForm.code || '' }}</view>
                </jk-form-item>
                <jk-form-item label="分类" :show-right-icon="false">
                    <view>{{ ruleForm.categoryName || '' }}</view>
                </jk-form-item>
                <jk-form-item label="品种" :show-right-icon="false">
                    <view>{{ ruleForm.productName || '' }}</view>
                </jk-form-item>
                <jk-form-item label="批号" :show-right-icon="false">
                    <view>{{ ruleForm.batchCode || '' }}</view>
                </jk-form-item>
                <jk-form-item label="成分" :show-right-icon="false">
                    <view>{{ ruleForm.ingredient || '' }}</view>
                </jk-form-item>
                <jk-form-item label="纸管" :show-right-icon="false">
                    <view>{{ ruleForm.paperTubeList ? ruleForm.paperTubeList.map(x => x.name).join() : '' }}</view>
                </jk-form-item>
                <jk-form-item label="外袋" :show-right-icon="false">
                    <view>{{ ruleForm.outerBagList ? ruleForm.outerBagList.map(x => x.name).join() : '' }}</view>
                </jk-form-item>
                <jk-form-item label="内袋" :show-right-icon="false">
                    <view>{{ ruleForm.innerBagList ? ruleForm.innerBagList.map(x => x.name).join() : '' }}</view>
                </jk-form-item>
                <jk-form-item label="扎线" :show-right-icon="false">
                    <view>{{ ruleForm.bundleList ? ruleForm.bundleList.map(x => x.name).join() : '' }}</view>
                </jk-form-item>
                <!--<jk-form-item label="备注" :show-right-icon="false">
                    <view>{{ ruleForm.remark || '' }}</view>
                </jk-form-item>-->
            </jkFormCard>
            <view class="margin-top-30">
                <jkFormCard title="打印数量">
                    <input v-model="printData.printCount" :cursorSpacing="120" type="number" @input="onIptChange" border style="border: solid 1rpx #eee;border-radius: 8rpx;height: 84rpx;padding: 0 24rpx;" @blur="onBlur">
                </jkFormCard>
            </view>
        </view>
		<view class="fixed-footer">
			<button @click="onConfirmBtn" type="primary" :disabled="!ruleForm.code || !printData.printCount || !deviceState" :loading="confirmLoading" style="width: 100%;" size="large">确定打印</button>
		</view>
		<u-datetime-picker
            v-model="selectDateTime"
            :show="selectDateTimePickerState"
            mode="datetime"
			:closeOnClickOverlay="true"
            @confirm="onConfirmBtnOfSelectPicker"
            @close="onCloseBtnOfSelectPicker"
			@cancel="onCloseBtnOfSelectPicker"
        ></u-datetime-picker>
	</scroll-view>
</template>

<script>
    import {
        productionPackagePlanGet,
        yongYouInventoryProductStockList, yongYouWarehouseList
    } from "../../../../api";
    import {
        formatTime,
        showLoading
    } from "../../../../utils/jk-utils";
        import jkPopup from '../../../../jk-components/jk-popup';
    import jkFormCard from "../../../../jk-components/jk-from-card";
    import jkFormItem from "../../../../jk-components/jk-from-item";
    import jkPopupOption from "../../../../jk-components/jk-popup-option";
    import blueList from '../../electronicWeighing/blue-list';
    let _this;
	export default {
		components: {
            blueList,
            jkPopupOption,
            jkFormCard,
            jkFormItem,
			jkPopup
		},
		data() {
			return {
                printData: {
                    printCount: 1,
                    data: {}
                },
                printRandom: 0,
                realTimeVal: '',
                firstFocus: true,
				selectDateTime: '',
				selectDateTimePickerState: false,
				confirmLoading: false,
				activeTab: 0,
				tabList: ['基本信息', '包装信息'],
				dataId: null,
				ruleForm: {
                    "batchCode": "",
                    "batchId": 0,
                    "beforeNumber": 0,
                    "bundleList": [],
                    "empCode": "",
                    "empId": 0,
                    "empName": "",
                    "innerBagList": [],
                    "invalidDateTime": "",
                    "number": 0,
                    "outerBagList": [],
                    "packingPlanCode": "",
                    "packingPlanId": 0,
                    "paperTubeList": [],
                    "productCode": "",
                    "productId": 0,
                    "productName": "",
                    "productionDateTime": "",
                    "remark": "",
                    "suttle": 0,
                    "type": 0,
                    "warehouseCode": "",
                    "warehouseId": 0,
                    "warehouseName": "",
                    "workshopCode": "",
                    "workshopId": 0,
                    "workshopName": ""
				},
                operationType: '1',
                deviceState: false
			}
		},
		onLoad(e) {
            this.operationType =  e.type;
            this.dataId = e.id;
		},
		onShow() {
            _this = this;
			this.getDependentData();
		},
		methods: {
            getDeviceState(e) {
                this.deviceState = e;
            },
            onConfirmBtn() {
                this.printRandom = Math.random();
                this.$set(this.printData, 'data', JSON.parse(JSON.stringify(this.ruleForm)));
                console.log('打印数', this.printData.printCount)
                this.confirmLoading = true;
            },
		    getBlueVal(v) {
                console.log('得到', v)
                // 修正this，否则指向会有问题
                _this.realTimeVal = v;
                wx.showToast({
                    title: `称重数据：${_this.realTimeVal}`,
                    icon: 'success'
                })
            },
            onBlur() {
                // this.firstFocus = false;
                // console.log('失去焦点')
            },
            onIptChange(e) {
                console.log('监听：', e.detail.value, typeof e.detail.value, Number(e.detail.value))
                this.$set(this.printData, 'printCount', Number(e.detail.value));
            },
            onClearWarehouse() {
                this.$set(this.ruleForm, 'warehouseId', '');
                this.$set(this.ruleForm, 'warehouseCode', '');
                this.$set(this.ruleForm, 'warehouseName', '');
            },
            onSelectTime(type) {
                this.timeType = type;
                this.selectDateTimePickerState = true;
            },
            onScanBtn() {
                let _this = this;
                // 允许从相机和相册扫码
                uni.scanCode({
                    success: function (res) {
                        let scanData = '';
                        try {
                            scanData = res.result;
                        } catch {
                            wx.showToast({
                                title: '无效的二维码',
                                icon: 'error'
                            })
                        }
                        if (scanData) {
                            wx.showModal({
                                title: '扫码结果',
                                content: `识别到计划:${scanData}`,
                                success(res) {
                                    if (res.confirm) {
                                        _this.getPlanDetailRequest(scanData);
                                    }
                                }
                            })
                        } else {
                            wx.showToast({
                                title: '二维码格式不符',
                                icon: 'error'
                            })
                        }
                    }
                });

            },
            getPlanDetailRequest(code) {
                return productionPackagePlanGet({ code }).then(res => {
                    if (res.data.status === 200) {
                        if (res.data.res) {
                            this.ruleForm = res.data.res;
                        } else {
                            this.ruleForm = {};
                            wx.showToast({
                                title: '无效的二维码',
                                icon: 'error'
                            });
                        }
                    }
                });
            },
            onTransferNumber(e, type) {
                this.$set(this.ruleForm, type, Number(e));
            },
		    onDeleteTime() {
                this.$set(this.ruleForm, 'startSystemTime', null);
            },
            onOutputRatioBlur(e) {
                if (e > 1) {
                    this.$set(this.ruleForm, 'outputRatio', 1);
                }
            },
			onUpdateTime() {
				this.selectDateTimePickerState = true;
			},
			onCloseBtnOfSelectPicker() {
				this.selectDateTimePickerState = false;
			},
			onConfirmBtnOfSelectPicker(e) {
				this.$set(this.ruleForm, this.timeType, formatTime(e.value));
				this.selectDateTimePickerState = false;
			},
			onTabChange(e) {
				this.activeTab = e;
			},
            getDetailRequest() {
                return productionPackagePlanGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res;
                    }
                });
            },
			async getDependentData() {
				showLoading(true);
				this.selectDateTime = formatTime(Date.now());
                if (this.dataId) await this.getDetailRequest();
                // this.$set(this.ruleForm, 'invalidDateTime', formatTime(Date.now()));
                // this.$set(this.ruleForm, 'productionDateTime', formatTime(Date.now()));
				showLoading(false);
			}
		}
	}
</script>

<style>

</style>
<style lang="scss" scoped>
	.machine-title {
		height: 42px;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #2d8cf0;
		border-radius: 12rpx;
		padding: 12rpx 24rpx;
		box-sizing: border-box;
        border: solid 1px #2d8cf0;
		text-align: center;
		font-size: 34rpx;
		font-weight: 500;
		margin: 10rpx;
	}

	.popup-content-box {
		padding: 32rpx;

		.popup-content-box-title {
			font-size: 34rpx;
			text-align: center;
			margin-bottom: 20rpx;
		}
	}
</style>
